

capture log close
log using $log/EDS_balancing_01, text replace

clear
clear matrix
clear mata
set mem 2g
set matsize 800
set more off
set maxvar 20000
set trace off
set varabbrev off
set seed 123342336
	 
	 
#d;
		global labor_market "loq_aloquote
		log_auslalo
		log_erwquote
		log_vacrate log_gdp 
		log_selfempl
		log_insolv_rate
		log_einksteuer log_steuerkraft log_gewsteuer 
		log_manufacturing log_service log_agriculture
		
		";
		
		global trends "aloq_delta1 aloq_delta3 vacrate_detla1 vacrate_detla3 gdp_delta1 gdp_delta3
		erwquote_delta1 erwquote_delta3";
		
		global policy "log_intensity_fbw log_intensity_abm log_intensity_sus
		log_intensity_jsa log_sanction_rate";
		
		global population "log_fertility 
		log_einw1830 log_einw3049 log_einw5064 log_einw65
		log_auslanteil log_schuleHS log_schuleABI 
		log_zuzug log_mobirate
		log_hheink  
		";
		#d cr
		
		
		
	forvalues spec=1/2 { 
	 use $data/data_pair, clear
		
		
 
		gen bula = floor(region_kkz/1000)
		tab bula, g(bula_)

  
	    if `spec'==1 local ivres "log_intensity_alt"
	  
		if `spec'==2 {
		qui reg log_intensity_alt county_pair_* bula_* year [pw=1/fweight_reg] , cluster(region)
		predict residual_int_temp, r
		bys region: egen residual_int = mean(residual_int_temp) 
	  	local ivres "residual_int"
		}
				
		foreach var in  $labor_market $trends $population $policy {
		bys region: egen `var'_m = mean(`var')
		drop `var'
		rename `var'_m `var'
		}
		bys region: keep if _n==1
		
	   foreach var in $labor_market $trends $policy $population {
	   reg `ivres' `var', cluster(region)
	   test `var'
	   scalar p = r(p)
	   
	   corr `ivres' `var'
	   scalar b=r(rho)
	   matrix d_`var'=b,p
		}
		
		
		reg `ivres' $labor_market $trends $policy $population year, cluster(region)
	    test $labor_market
		matrix pvalue_lm = r(p),.
		
		test $trends
		matrix pvalue_trends = r(p),.
		
		
	    test $policy
		matrix pvalue_policy = r(p),.
	
	    test $population
		matrix pvalue_population = r(p),.
		
		
		
		#d;	
		matrix d`spec' = d_loq_aloquote
		\d_log_erwquote\d_log_vacrate\d_log_gdp\d_log_auslalo 
		\d_log_selfempl\d_log_insolv_rate
		\d_log_einksteuer\d_log_steuerkraft\d_log_gewsteuer 
		\d_log_manufacturing\d_log_service\d_log_agriculture
		\d_aloq_delta1\d_aloq_delta3
		\d_erwquote_delta1\d_erwquote_delta3
		\d_vacrate_detla1\d_vacrate_detla3
		\d_gdp_delta1\d_gdp_delta3
		\d_log_einw1830\d_log_einw3049\d_log_einw5064\d_log_einw65
		\d_log_fertility
		\d_log_auslanteil\d_log_schuleHS\d_log_schuleABI 
		\d_log_zuzug\d_log_mobirate
		\d_log_hheink  	
		\d_log_intensity_fbw\d_log_intensity_abm\d_log_intensity_sus
		\d_log_intensity_jsa\d_log_sanction_rate
		\pvalue_lm \ pvalue_trends \ pvalue_population \ pvalue_policy
		;
		#d cr
		
		
		 }
	  
	  matrix d=d1,d2
	  
	  #d;
	matrix rownames d = 
	"Unemployment rate"  "Employment rate" "Vacancy rate" "GDP per capita"
	"Unemployment rate foreigners" "Selfemployment rate" "Insolvency rate"
	"Income tax per capita" "Tax revenue per capita" "Business tax per capita"
	"Manufacturing" "Service" "Agriculture"
	"Change UE last year" "Change UE last three year"
	"Change emp last year" "Change emp last three year"
	"Change vac last year" "Change vac last three year"
	"Change gdplast year" "Change gdp last three year"
	"18-29 years" "30-49 years" "50-64 years" "65 years and above"
	"Fertility rate" "Share foreigners" "Share with lower sec degree"
	"Share with upper sec degree" "Immigration rate" "Emigration rate"
	"Average household income" "Training programs" "Workfare programs" "Start-up subsidies"
	"Job search assistance" "Sanctions" "P-value labor market" "P-value trends" "P-value population" "P-value policy"
		;
	#delimit cr

	matrix colname d = "Unconditional" "Conditional"
	mat list d
	estout matrix(d, fmt(3)) using $data/Table_2.tex, style(tex) replace 
	
		
	    
	
	 
	   
		log close
	 
	

